package com.shensi.adminCollect.controller.integral;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.integral.CustomerPoints;
import com.shensi.adminCollect.service.integral.ICustomerPointsService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import com.shensi.adminCollect.service.system.ISysStationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 用户积分Controller
 *
 * @author qujia
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/integral/points")
public class CustomerPointsController extends BaseController
{
    @Autowired
    private ICustomerPointsService customerPointsService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysStationUserService stationUserService;

    /**
     * 查询用户积分列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                customerPoints.setStationIdList(list);
                customerPoints.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            customerPoints.setStationId(currentUser.getUser().getStationId());
        }
        startPage();
        List<CustomerPoints> customerPointsList = customerPointsService.selectCustomerPointsList(customerPoints);
        return getDataTable(customerPointsList);
    }

    /**
     * 导出用户积分列表
     */
    @GetMapping("/export")
    public AjaxResult export(CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                customerPoints.setStationIdList(list);
                customerPoints.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            customerPoints.setStationId(currentUser.getUser().getStationId());
        }
        List<CustomerPoints> customerPointsList = customerPointsService.selectCustomerPointsList(customerPoints);
        ExcelUtil<CustomerPoints> util = new ExcelUtil<CustomerPoints>(CustomerPoints.class);
        return util.exportExcel(customerPointsList, "points");
    }

    /**
     * 获取用户积分详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerPointsService.selectCustomerPointsById(id));
    }

    /**
     * 新增用户积分
     */
    @PostMapping
    public AjaxResult add(@RequestBody CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPoints.setStationId(currentUser.getUser().getDeptId());
        customerPoints.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsService.insertCustomerPoints(customerPoints));
    }

    /**
     * 修改用户积分
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPoints.setStationId(currentUser.getUser().getDeptId());
        customerPoints.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsService.updateCustomerPoints(customerPoints));
    }

    /**
     * 删除用户积分
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerPointsService.deleteCustomerPointsByIds(ids));
    }
}
